import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  Put,
} from '@nestjs/common';
import { WorkerService } from './worker.service';
import { CreateWorkerDto } from './dto/create-worker.dto';
import { UpdateWorkerDto } from './dto/update-worker.dto';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { QueryPageDto } from '../../common/dto/query.page.dto';
import { QueryModelDto } from '../model/dto/query-model.dto';
import { Prisma } from '@prisma/client';
import { QueryWorkerDto } from './dto/query-worker.dto';
import { Roles } from '../../common/decorator/roles.decorator';
import { Role } from '../../common/types/role';
import { RolesGuard } from '../../common/guards/roles.guard';

@Controller('worker')
@ApiTags('员工管理')
@Roles(Role.FINANCE)
@UseGuards(RolesGuard)
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
export class WorkerController {
  constructor(private readonly workerService: WorkerService) {}

  @Post()
  create(@Body() createWorkerDto: CreateWorkerDto) {
    return this.workerService.createModel<CreateWorkerDto>(createWorkerDto);
  }

  @Post('page')
  findByPage(@Body() query: QueryPageDto<QueryWorkerDto>) {
    const where: Prisma.modelWhereInput = {
      name: {
        contains: query.model.name || undefined,
      },
      code: {
        contains: query.model.code || undefined,
      },
    };

    return this.workerService.queryPage(query, where);
  }

  @Get()
  findAll() {
    return this.workerService.findAll();
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.workerService.findOne(id);
  }

  @Put(':id')
  update(@Param('id') id: string, @Body() updateWorkerDto: UpdateWorkerDto) {
    return this.workerService.updateModel(id, updateWorkerDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.workerService.remove(id);
  }
}
